#include "mypacketmodel.h"
#include "mainwindow.h"
#include "..\MyPacketProcessor\exports.h"
//#include "..\USBSniffer\public.h"
#include <deque>

extern std::deque<PPACKET> bufferDeque;
extern HANDLE hMutex;

extern bool stopThreads;

extern QList<MyPacket*> packetList;
extern MyPacketModel pModel;


DWORD WINAPI PacketGeneratorThread(LPVOID lpParameter)
{
	DWORD dwWaitResult;
	char *pBuffer = NULL;
	PPACKET pPacket = NULL;
	MyPacket *pMyPacket = NULL;

	while (true)
	{
		dwWaitResult = WaitForSingleObject(hMutex, 5000L);
			if (dwWaitResult == WAIT_TIMEOUT)
			{
				return 1;
			}
			if (stopThreads && bufferDeque.empty())
			{
				break;
			}
			if (!bufferDeque.empty())
			{
				pPacket = bufferDeque.front();
				bufferDeque.pop_front();
			}
		ReleaseMutex(hMutex);
 		if (pPacket == NULL)
 		{
 			continue;
 		}
		pBuffer = pPacket->Buffer;
		if (pBuffer != NULL)
		{
			pMyPacket = ProcessIncomingBuffer(pBuffer, SIZE_OF_PACKET);
			if (pMyPacket != NULL)
			{
				packetList.append(pMyPacket);
				pModel.Update();
			}
			GlobalFree(pPacket);
			pPacket = NULL;
		}	
	}

	return 0;
}